<?php

namespace App\Dao;

use App\Model\UserRemark;
use App\Utils\MemcacheClient;
use Hyperf\Context\ApplicationContext;

class UserRemarkDao
{
    private static $instance;

    public static function getInstance()
    {
        if (!self::$instance) {
            self::$instance = Applicationcontext::getcontainer()->get(self::class);
        }
        return self::$instance;
    }


    /**
     * Note - 根据主播获取对应用户标签名数据
     * DateTime 2024/7/19 12:31
     * User Admin
     * @param $id
     * @return array
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function getOne($id)
    {
        $data = $this->getData($id);
        $uids = [];
        if (!empty($data)) {
            foreach ($data as $v) {
                $uids[$v['uid']] = $v;
            }
        }
        return !empty($uids) ? $uids : [];
    }

    /**
     * Note - 获取该主播备注数据
     * DateTime 2024/7/19 12:31
     * User Admin
     * @param $id
     * @param $cache
     * @return mixed|mixed[]
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function getData($id, $cache = true)
    {
        $data = MemcacheClient::getInstance()->get('anchor_user_remarks_' . $id);
        if (empty($data) || $cache === false) {
            $data = UserRemark::where('anchor_id', $id)->get()->toArray();
            MemcacheClient::getInstance()->set('anchor_user_remarks_' . $id, $data, 3600);
        }
        return $data;
    }


    /**
     * Note - 获取该主播对应用户的备注名
     * DateTime 2024/7/19 12:31
     * User Admin
     * @param $uid
     * @param $anchor_id
     * @param $cache
     * @return mixed
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function getInfo($uid, $anchor_id, $cache = true)
    {
        $data = MemcacheClient::getInstance()->get('remarks_' . $uid . '_' . $anchor_id);
        if (empty($data) || $cache === false) {
            // $sql = "select * from {$this->table} where anchor_id = ? and uid = ? ";
            // $pd = $this->createCommand($sql);
            // $pd->execute(array($anchor_id, $uid));
            // $data = $pd->fetch(PDO::FETCH_ASSOC);
            $data = UserRemark::where('anchor_id', $anchor_id)->where('uid', $uid)->first();
            $data = modelToArray($data);
            MemcacheClient::getInstance()->set('remarks_' . $uid . '_' . $anchor_id, $data, 3600);
        }
        return $data;
    }


}